//
//  UserInfoCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/15.
//

import UIKit

class UserInfoCell: UITableViewCell {
    
    lazy var typeLalbel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 16)
        label.textAlignment = .left
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "white_F9F8F8")
        view.wn_cornerWithBorder(4)
        return view
    }()
    
    lazy var infoLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textAlignment = .left
        label.textColor = .black
        return label
    }()
    
    lazy var editBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.rx.tap.bind{ [weak self] in
            self?.editClosure?()
        }.disposed(by: rx.disposeBag)
        return btn
    }()

    lazy var nameView: IQTextView = {
        let view = IQTextView()
        view.textContainerInset = .init(top: 0, left: 0, bottom: 0, right: 0)
        view.font = .wn_font(name: .bold, size: 16)
        view.placeholder = "Please add your display name"
        view.textColor = .black
        view.tintColor = .init(named: "red_FE95B4")
        view.keyboardDismissMode = .none
        view.returnKeyType = .done
        view.backgroundColor = .clear
        view.textContainer.maximumNumberOfLines = 1
        view.isScrollEnabled = false
        view.isHidden = true
        return view
    }()
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.addSubview(typeLalbel)
        contentView.addSubview(bgView)
        bgView.addSubview(infoLabel)
        bgView.addSubview(editBtn)
        bgView.addSubview(nameView)
        typeLalbel.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(20)
            make.height.equalTo(16)
        }
        bgView.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.equalTo(0)
            make.right.equalTo(-12)
            make.height.equalTo(40)
        }
        infoLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(8)
        }
        editBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-6)
            make.size.equalTo(CGSize.init(width: 18, height: 18))
        }
        nameView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(8)
            make.right.equalTo(-30)
            make.height.equalTo(20)
        }
    }
    
    var editClosure: ReturnClosure?
    
    var model: UserDetailModel?
    
    var editType: Int? {
        didSet {
            nameView.isHidden = true
            switch editType {
            case 0:
                typeLalbel.text = "NICKNAME"
                infoLabel.text = ""
                nameView.resignFirstResponder()
                nameView.isHidden = false
                nameView.text = model?.realName
                editBtn.setImage(UIImage(named: "user_info_name"), for: .normal)
            case 1:
                typeLalbel.text = "SEX"
                infoLabel.text = model?.sex == 1 ? "Male" : (model?.sex == 2 ? "Female" : "Secrecy")
                editBtn.setImage(UIImage(named: "user_info_sex"), for: .normal)
            case 2:
                typeLalbel.text = "BIRTHDAY"
                infoLabel.text = model?.birthday
                if model?.birthday == "" {
                    infoLabel.text = "Set Up"
                }
                editBtn.setImage(UIImage(named: "user_info_birthday"), for: .normal)
            default:
                break
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
